package myky.com.doctorapp.ui



import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gyf.barlibrary.ImmersionBar
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_follow_doctor_list.*
import kotlinx.android.synthetic.main.activity_follow_doctor_list_adapter.*
import kotlinx.android.synthetic.main.activity_follow_record_list.*

import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.FollowRecordAdapter
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.FollowRecordListContract
import myky.com.doctorapp.entity.FollowRecordEntity
import myky.com.doctorapp.presenter.FollowRecordListPresenter
import myky.com.doctorapp.utils.Constants

/**
 * Description: 随访记录列表
 * @author wuyang
 * Created by 2019/3/19 17:36
 */
class FollowRecordListActivity : BaseActivity<FollowRecordListPresenter,FollowRecordEntity.DataBean>(),FollowRecordListContract.View ,OnRefreshLoadMoreListener,BaseQuickAdapter.OnItemChildClickListener{

    lateinit var recordList:MutableList<FollowRecordEntity.DataBean.FollowrecordlistBean>
    lateinit var followRecordAdapter: FollowRecordAdapter
    override fun getPresenter(): FollowRecordListPresenter = FollowRecordListPresenter()
    var current:Int=1
    var doctorId:Int=0
    lateinit var textView:TextView
    var positions:Int=-1


    override fun onLoadMore(refreshLayout: RefreshLayout) {
        current++
        mPresenter.pgetFollowRecordList(current,doctorId)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        current=1
        recordList.clear()
        mPresenter.pgetFollowRecordList(current,doctorId)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        var data = adapter.data[position] as FollowRecordEntity.DataBean.FollowrecordlistBean
        var bundle=Bundle()
        bundle.putSerializable(Constants.INTENTENTITY,data)
        var ifinput = data.ifinput
        positions=position
        when(view.id){

            R.id.record_btn->{
                if (ifinput==0){
                    startActivityForResult(FollowHtmlActivity::class.java,bundle,Constants.INTENTCODE)
                }
            }
            R.id.record_preview->{
                startActivity(FollowHtmlActivity::class.java,bundle)
            }
        }

    }


    override fun reloadActivity() {
    }

    override fun injectTarget(): View=findViewById(R.id.follow_record_list_lin)

    override fun initData() {
        var bundle = intent.extras
        if (bundle!=null){doctorId=bundle.getInt(Constants.DOCTORID)}
        mPresenter.pgetFollowRecordList(current,doctorId)
    }

    override fun initView() {
        ImmersionBar.with(this)
                .statusBarColor(R.color.white)
                .titleBar(R.id.follow_record_list_title)
                .init()
        conmonTitleTextView_record.text=getXmlString(R.string.follow_record_list)
        recordList=ArrayList()
        followRecordAdapter= FollowRecordAdapter(recordList)
        follow_record_recycler.layoutManager= LinearLayoutManager(this)
        val view = layoutInflater.inflate(R.layout.follow_record_header, null)
        textView = view.findViewById(R.id.follow_adapter_header_tv)
        followRecordAdapter.addHeaderView(view)
        follow_record_recycler.adapter=followRecordAdapter
        followRecordAdapter.onItemChildClickListener=this
        follow_record_refresh.setOnRefreshLoadMoreListener(this)
        backLayout_record.setOnClickListener { finish() }
    }

    override fun getContenView(): Int =R.layout.activity_follow_record_list

    override fun showDataSuccess(datas: FollowRecordEntity.DataBean) {
        var realnames = datas.realname
        var allrecordcount = datas.allrecordcount
        var completedrecord = datas.completedrecord
        textView.text=realnames + "医生给您发了" + allrecordcount + "份问诊表，已填写" + completedrecord + "份"

        if (datas==null|| datas.followrecordlist.isEmpty()){
            if (current==1){
                showEmptyView(getXmlString(R.string.new_case_popwindow_empty))
            }else{
                follow_record_refresh.finishLoadMoreWithNoMoreData()
            }
        }
        if (datas!=null) {
            recordList.addAll(datas.followrecordlist)
            followRecordAdapter.notifyDataSetChanged()
        }
        follow_record_refresh.finishLoadMore(10)
        follow_record_refresh.finishRefresh(10)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data != null && resultCode == RESULT_OK) {
            when (requestCode) {

                Constants.INTENTCODE -> {
                    val time = data.getLongExtra(Constants.RESULTDATE, 0)
                    recordList[positions].inputtime = time
                    recordList[positions].ifinput = 1
//                    头部需要+1
                    followRecordAdapter.notifyItemChanged(positions + 1)
                }
            }//算上头布局得+1
        }
    }
}
